<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            position: relative;
        }

        img {
            width: 400px;
            vertical-align: top;
            padding: 5px;
        }
    </style>
</head>
<body>
<!--https://www.cnblogs.com/lvonve/p/11820026.html-->
<div class="box">
    <img src="img/1.jpg" alt="">
    <img src="img/2.jpg" alt="">
    <img src="img/3.jpg" alt="">
    <img src="img/4.jpg" alt="">
    <img src="img/05.jpg" alt="">
    <img src="img/6.jpg" alt="">
    <img src="img/7.jpg" alt="">
    <img src="img/8.jpg" alt="">
    <img src="img/9.jpg" alt="">
    <img src="img/10.jpg" alt="">
    <img src="img/11.jpg" alt="">
    <img src="img/12.jpg" alt="">
    <img src="img/13.jpg" alt="">
    <img src="img/014.jpg" alt="">
    <img src="img/015.jpg" alt="">
    <img src="img/016.jpg" alt="">
</div>

<script src="js/jquery-2.0.0.min.js"></script>
<script>
    $(function () {
        let imgWidth = $('img').outerWidth();

        waterFall();

        function waterFall() {

            let column = parseInt($(window).width() / imgWidth)
            let heightArr = [];
            for (let i = 0; i < column; i++) {
                heightArr[i] = 0;
            }

            $.each($('img'), function (index, item) {
                let itemHeight = $(item).outerHeight();
                let minHeight = Math.min(...heightArr)
                let minIndex = heightArr.indexOf(minHeight)
                $(item).css({
                    position: 'absolute',
                    top: minHeight + 'px',
                    left: minIndex * imgWidth + 'px'
                });
                console.log("index "+index)
                console.log("itemHeight "+itemHeight)
                console.log("heightArr "+heightArr)
                console.log("minHeight "+minHeight)
                console.log("minIndex "+minIndex)
                console.log("top "+minHeight)
                console.log("left "+minIndex * imgWidth)

                heightArr[minIndex] += itemHeight;


                console.log("heightArr "+heightArr)

            })

        }

        $(window).resize(function () {
            waterFall();
        })
    })
</script>

</body>
</html>